package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.AssignHomework;
import com.production.CoursePlatform.service.AssignHomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assignHomework")
public class AssignHomeworkController {
    //此接口已检验
    @Autowired
    private AssignHomeworkService assignHomeworkService;

    // 创建新作业
    @PostMapping("/create")
    public ResponseEntity<String> createAssignHomework(@RequestBody AssignHomework assignHomework) {
        System.out.println(assignHomework);
        int result = assignHomeworkService.createAssignHomework(assignHomework);
        if (result > 0) {
            return ResponseEntity.ok("作业创建成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("作业创建失败");
        }
    }

    // 按 ctID 查询作业
    @GetMapping("/findByCtID/{ctID}")
    public ResponseEntity<List<AssignHomework>> findByCtID(@PathVariable int ctID) {
        List<AssignHomework> homeworks = assignHomeworkService.findByCtID(ctID);
        return ResponseEntity.ok(homeworks);
    }

    // 删除作业
    @DeleteMapping("/delete/{ahID}")
    public ResponseEntity<String> deleteAssignHomework(@PathVariable int ahID) {
        int result = assignHomeworkService.deleteAssignHomework(ahID);
        if (result > 0) {
            return ResponseEntity.ok("作业删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("作业删除失败");
        }
    }

    // 更新作业
    @PutMapping("/update")
    public ResponseEntity<String> updateAssignHomework(@RequestBody AssignHomework assignHomework) {
        int result = assignHomeworkService.updateAssignHomework(assignHomework);
        if (result > 0) {
            return ResponseEntity.ok("作业更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("作业更新失败");
        }
    }
}

